package com.xjrsoft.module.oa.controller;


import com.xjrsoft.common.Enum.EnabledMarkEnum;
import com.xjrsoft.common.result.Response;
import com.xjrsoft.core.tool.utils.BeanUtil;
import com.xjrsoft.core.tool.utils.StringPool;
import com.xjrsoft.module.base.dto.GetPageListDto;
import com.xjrsoft.module.oa.dto.NewsDto;
import com.xjrsoft.module.oa.dto.NewsPageDto;
import com.xjrsoft.module.oa.entity.XjrOaNews;
import com.xjrsoft.module.oa.service.IXjrOaMessageService;
import com.xjrsoft.module.oa.service.IXjrOaNewsService;
import com.xjrsoft.module.oa.utils.SendMessageUtil;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * <p>
 * 新闻中心表 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2020-11-10
 */
@RestController
@AllArgsConstructor
@RequestMapping("/notice")
public class XjrOaNoticeController {
    private IXjrOaNewsService newsService;


    @Autowired
    private IXjrOaMessageService iXjrOaMessageService;

    @ApiOperation(value="获取公告分页")
    @GetMapping
    public Response getPageList(NewsPageDto dto) {
        return Response.ok(newsService.getPageList(2, dto));
    }

    @ApiOperation(value="新增公告")
    @PostMapping
    public Response addNews(@RequestBody NewsDto newsDto) {
        XjrOaNews oaNews = BeanUtil.copy(newsDto, XjrOaNews.class);
        oaNews.setEnabledMark(EnabledMarkEnum.NOENABLED.getCode());
        boolean save = newsService.save(oaNews);
        return Response.status(save);
    }

    @ApiOperation(value="修改公告")
    @PutMapping("/{id}")
    public Response updateNews(@PathVariable String id, @RequestBody NewsDto newsDto) {
        XjrOaNews oaNews = BeanUtil.copy(newsDto, XjrOaNews.class);
        oaNews.setNewsId(id);
        return Response.status(newsService.updateById(oaNews));
    }

    @ApiOperation(value="删除公告")
    @DeleteMapping("/{ids}")
    public Response deleteNews(@PathVariable String ids) {
        String[] idArray = StringUtils.split(ids, StringPool.COMMA);
        boolean isSuccess = false;
        if (idArray.length == 1) {
            isSuccess = newsService.removeById(ids);
        } else {
            isSuccess = newsService.removeByIds(Arrays.asList(idArray));
        }
        for (String id : idArray) {
            iXjrOaMessageService.deleteNewsMessage(id);
        }
        return Response.status(isSuccess);
    }

    @PutMapping("/changeStatus")
    @ApiOperation(value="发布或下架公告")
    public Response changeStatus(@RequestParam("id") String id,@RequestParam("type") Integer type) throws Exception {
        XjrOaNews xjrOaNews = newsService.getById(id);
        if(type==null||xjrOaNews==null){
            throw new Exception("参数异常");
        }
        if (type==xjrOaNews.getEnabledMark()) {
            if (type == 1) {
                throw new Exception("公告已发布!!!");
            } else if (type == 0) {
                throw new Exception("公告已下架!!!");
            }
        }
        if(type==0){
            //删除新闻消息
            iXjrOaMessageService.deleteNewsMessage(id);
        }else {
            //新增消息
            SendMessageUtil.sendNewsMessage(xjrOaNews,11);
        }
        xjrOaNews.setEnabledMark(type);
        return Response.status(newsService.updateById(xjrOaNews));
    }
}
